//
//  LJWelcomeViewController.swift
//  LJWeibo
//
//  Created by Raymond on 15/12/17.
//  Copyright © 2015年 liu. All rights reserved.
//

import UIKit
import SDWebImage


class LJWelcomeViewController: UIViewController {

    // 头像底部约束
    var iconViewCon: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //准备UI
        self.prepareUI()
        
        // 准备用户之前的头像
        setupIconView()
        
        // 加载新的头像
        LJUserAccount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            if error != nil{
                print("获取用户信息出错了")
                return
            }
            
            self.setupIconView()
        })
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconViewCon?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 7, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            // 更新约束
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                // 显示label
                self.welcomeLabel.alpha = 0
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    self.welcomeLabel.hidden = false
                    }, completion: { (_) -> Void in
                        (UIApplication.sharedApplication().delegate as! AppDelegate).swtichRootViewController(true)
                })
        }
    }
    
    

    func prepareUI(){
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 添加约束
        // 背景约束
        bkgImageView.ff_Fill(view)
        
        // 头像约束
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSizeMake(85, 85), offset: CGPointMake(0, -160))
        iconViewCon = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        // 文字约束
         welcomeLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPointMake(0, 16))
        
    }
    
    private func setupIconView(){
        if let avatar_large = LJUserAccount.userAccount?.avatar_large{
            let url = NSURL(string: avatar_large)
            
            iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    // MARK: - 懒加载
    /// 背景图
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像
    private lazy var iconView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        //剪切为圆
        view.layer.cornerRadius = 42.5
        view.layer.masksToBounds = true
        
        return view
    }()
    
    ///文字
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.font = UIFont.systemFontOfSize(14)
        label.hidden = true
        return label
    }()
    

    

}
